<template>
  <div class="login-container">
    <el-form :model="form" >
      <el-form-item label="用户名" >
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码" >
        <el-input v-model="form.password" />
      </el-form-item>
      <el-form-item >
        <el-button type="primary"  @click="login" >登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref} from 'vue'
import { useRouter } from 'vue-router'
import { useCounterStore } from "@/stores/counter.js";
import { ElMessage } from "element-plus";

// 在 setup 中使用
const router = useRouter()
import service from '@/utils/request'

let form = ref({username:'',password:''})
let counter = useCounterStore()
function login(){
  service.post("/user/login?username="+form.value.username+"&password="+form.value.password).then(
      res=>{
        if(res.data){
          //1.将userid存入pinia
          counter.vo_user.value = res.data
          //2.路由跳转
          router.replace("/")
          ElMessage({
            message: '登录成功!',
            type: 'success',
          })
        }
        else{
          //给提示
          ElMessage({
            message: '用户名或者密码错误!',
            type: 'error',
          })
        }
      }
  )
}
</script>
<style scoped>
  .login-container{
    margin: 100px auto ;
    width: 400px;
    height: 300px;
  }
/* 样式 */
</style>